/*

Copyright (c) 2006 gpx@niniu.com.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the name of the copright holder nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using System;
namespace BENBUN.ExportCRS
{
    public class Geom
    {
        private const double Rad2Deg = 360 / (2 * Math.PI);
        private const double Deg2Rad = (2 * Math.PI) / 360;
        private const double HalfPI = Math.PI / 2.0;

        public Geom()
        {
        }

        public static double DegToRad(double deg)
        {
            return deg * Deg2Rad;
        }

        public static double RadToDeg(double rad)
        {
            return rad * Rad2Deg;
        }

        public static double Angle0To360(double deg)
        {
            deg = deg % 360.0;
            return deg < 0.0 ? deg + 360.0 : deg;
        }

        public static double Angle180To180(double deg)
        {
            deg = Angle0To360(deg);
            return deg >= 180.0 ? deg - 360.0 : deg;
        }

        public static double DeltaBearing(double bearing, double refBearing)
        {
            double d = bearing - refBearing;

            if (d > +180.0) d -= 360.0;
            else if (d < -180.0) d += 360.0;

            return d;
        }

        public static void DistanceBearing(double latA, double lonA, double latB, double lonB, out double distance, out double bearing)
        {
            if (latA == latB && lonA == lonB)
            {
                distance = 0.0;
                bearing = 0.0;
                return;
            }

            double sinLatA = Math.Sin(DegToRad(latA));
            double sinLatB = Math.Sin(DegToRad(latB));

            double cosLatA = Math.Cos(DegToRad(latA));
            double cosLatB = Math.Cos(DegToRad(latB));

            double lonAB = Angle180To180(lonB - lonA);
            double cosLonAB = Math.Cos(DegToRad(lonAB));

            double tanLatB = Math.Tan(DegToRad(latB));
            double sinLonAB = Math.Sin(DegToRad(lonAB));

            double cosAB = (sinLatA * sinLatB) + (cosLatA * cosLatB * cosLonAB);
            distance = RadToDeg(60 * Math.Acos(cosAB)); // nautical miles!
            double temp = (cosLatA * tanLatB) - (sinLatA * cosLonAB);
            
            bearing = Math.Atan2(sinLonAB, temp);

            if (temp == 0.0 && lonAB > 0) bearing = HalfPI;
            else 
                if (temp == 0.0 && lonAB < 0) bearing = HalfPI + Math.PI;

            bearing = Angle0To360(RadToDeg(bearing)); //degrees

            if (Double.IsNaN(distance))
            {
                distance = 0.0;
            }
        }

        /// <summary>
        /// Is point x,y left or right of the line x1y1->x2y2?
        /// 
        /// lr == -1 means x,y left of line
        /// lr ==  0	means x,y is on the line
        /// lr ==  1	means x,y right of line
        /// 
        /// </summary>
        public static int LeftOrRight(double x1, double y1, double x2, double y2, double x, double y)
        {

            double a = y2 - y1;
            double b = -(x2 - x1);
            double c = -(a * x1 + b * y1);
            double d = a * x + b * y + c;

            if (d < -1e-9) return -1;
            else if (d > 1e-9) return 1;

            return 0;
        }

        public static double Interpolate(double x1, double x2, double a1, double a2, double xi)
        {
            if (x2 != x1) return a1 + (xi - x1) / (x2 - x1) * (a2 - a1);
	        
		    return 0.5 * (a1 + a2);
        }
    }
}
